
import java.awt.Color;
import java.awt.Graphics;
import java.util.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author theevilelephant
 */
public class ViewPort {
    private Page page;
    private int viewStart = 0;
    private int viewEnd = 0;
    private int viewPortSize;
    private boolean viewFinished = false;
    private boolean finished = false;
    private int lineC = 0;
    private int charC = 0;
   // private List view;


    private int pHeight;
    private int pWidth;
    private int fontSize;
    private int spacing;

    public ViewPort(Page p, int pHeight, int pWidth, int fontSize, int spacing){
        page = p;
        this.pHeight = pHeight;
        this.pWidth = pWidth;
        this.fontSize = fontSize;
        this.spacing = spacing;

        this.viewPortSize = pHeight/(fontSize+spacing);

        if(page.getNumLines() <= viewPortSize){
            viewEnd = page.getNumLines()-1;
        }else{
            viewEnd = viewPortSize-1;
        }
    }

    public void updateViewPort(){
        if(!finished){
            if(!viewFinished){
                if( charC < page.getLine(viewStart+lineC).getLength()){
                    charC++;
                }
                else{
                    charC = 0;

                    if((viewStart+lineC) < viewEnd){
                        //System.out.println(viewStart+lineC);
                        lineC++;
                    }
                    else{
                        viewFinished = true;
                    }
                }
            }
            else{
                movePageDown();
               // viewFinished = false;
               // finished = true;
            }
        }
    }

    private void movePageDown(){
        if(viewEnd != page.getNumLines()-1){
           viewFinished = false;
           lineC = 0;
           charC = 0;
           viewStart +=viewPortSize;
           if(viewEnd+viewPortSize>page.getNumLines()){
               viewEnd = page.getNumLines()-1;
           }
           else{
               viewEnd +=viewPortSize;
           }
        }
        else{
             finished = true;
        }
    }

    public boolean isFinished(){
        return finished;
    }

    public void Render(Graphics dbg){
        // clear the background
        dbg.setColor(Color.black);
        dbg.fillRect(0, 0, pWidth, pHeight);

        dbg.setColor(Color.green);

        int y;
        for( int i = 0; i <= lineC; i++){
            y = 10 + (i*(spacing+fontSize));

            if((i == lineC)&&(viewFinished==false)){
                //System.out.println(viewFinished);
                for( int j = 0;j < charC; j++){
                    dbg.drawString(""+page.getLine(viewStart+i).getChar(j), 10+j*10,y);
                }
                //cursor
                 dbg.drawLine(10+charC*10, y-10, 10+charC*10, y);
            }
            else{
                for( int j = 0;j < page.getLine(viewStart+i).getLength(); j++){
                    dbg.drawString(""+page.getLine(viewStart+i).getChar(j), 10+j*10,y);;
                }
            } 
        }
    }
}
